package com.util;

import com.javabean.User;
import mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;

public class UserMapperTest {
    SqlSessionFactory sqlSessionFactory;
    SqlSession sqlSession;
    UserMapper mapper;

    @Before
    public void before() {
        sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
        sqlSession = sqlSessionFactory.openSession(true);
        mapper = sqlSession.getMapper(UserMapper.class);
    }

    @After
    public void after() {
        sqlSession.close();
    }

    @Test
    public void selectAll(){
        for (User user : mapper.selectAll()) {
            System.out.println(user);
        }
    }
    @Test
    public void selectByUser(){
        User u=new User(1,null,null);
        for (User user : mapper.selectByUser(u)) {
            System.out.println(user);
        }
    }
    @Test
    public void selectByUserOneParam(){
        User u=new User(1,"aaaaaa","1211");
        for (User user : mapper.selectByUserOneParam(u)) {
            System.out.println(user);
        }
    }
    @Test
    public void updateUserByUid(){
        User u=new User(3,null,"bzssb","bzssb");
         mapper.updateUserByUid(u);
    }

    @Test
    public void insertUserList(){
        ArrayList<User> userList=new ArrayList<>();
        Collections.addAll(userList,
                new User(0,"hbz1","dsb","hbzdsb"),
                new User(0,"hbz2","dsb","hbzdsb"),
                new User(0,"hbz3","dsb","hbzdsb"),
                new User(0,"hbz4","dsb","hbzdsb"),
                new User(0,"hbz5","dsb","hbzdsb")
        );

    mapper.insertUserList(userList);
    }

    @Test
    public void deleteByArray(){
        mapper.deleteByArray(new int[]{4,5,6,7,8,9});
    }

}
